package com.polaris.live.widget.circle

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.blankj.utilcode.util.ColorUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.WidgetCircleFreeGiftBinding
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UserFreeGiftResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils

/**
 * CircleFreeGiftView
 *
 * @author Created by 半仙 on 2024/11/27/027 16:34
 */
class CircleFreeGiftView(context: Context, attributeSet: AttributeSet) : FrameLayout(context, attributeSet) {

    private val mBinding = WidgetCircleFreeGiftBinding.inflate(LayoutInflater.from(context), this)

    private var onClickGiftListener: ((UserFreeGiftResp) -> Unit)? = null
    fun setOnClickGiftListener(l: (UserFreeGiftResp) -> Unit) {
        onClickGiftListener = l
    }

    private var onShowViewCallback: (() -> Unit)? = null
    fun setOnShowViewCallback(l: () -> Unit) {
        onShowViewCallback = l
    }

    init {
        visibility = View.GONE
    }

    fun initFreeGift(sourceType: Int? = null, viewModel: BaseViewModel) {
        if (sourceType == null) {
            visibility = View.GONE
            return
        }
        viewModel.request({ userService.freeGiftInfo(sourceType) }, { resp ->
            if (resp.giftVo == null) {
                visibility = View.GONE
            } else {
                timeUi(resp)
                if(resp.count==1){ //数量为1时弹出第一次引导
                    onShowViewCallback?.invoke()
                }
                setOnSingleClickListener {
                    if (mBinding.circleRun.isRunning() || resp.count == 0) {
                        ToastGravity.showGravity(string.countdown_ovel_send_gift)
                    } else {
                        onClickGiftListener?.invoke(resp)
                    }
                }
                mBinding.circleRun.setAnimationCallback {
                    //圆环倒计时结束重新获取一次免费礼物
                    initFreeGift(sourceType, viewModel)
                }
                visibility = View.VISIBLE
            }
        }, {
            visibility = View.GONE
        })
    }

    private fun timeUi(userFreeGiftResp: UserFreeGiftResp) {
        val time = (userFreeGiftResp.availableTime ?: 0) - CacheUtil.getServerTime()

        if (userFreeGiftResp.availableTime != null && time > 0 && userFreeGiftResp.duration != null) {
            val allTime = userFreeGiftResp.duration * 1000
            val beforeTime = allTime - time //计算已经过去的时间
            val pr = beforeTime / allTime //计算进度百分比
            val showValues = (360 * pr).toFloat()
            mBinding.circleRun.anewRun(
                time,
                6f,
                ColorUtils.getColor(R.color.color_aaf000),
                Color.TRANSPARENT,
                showValues,
                true
            )
        } else {
            mBinding.circleRun.setProgress(360f, 6f, Color.TRANSPARENT, Color.TRANSPARENT)
        }
        Glide.with(mBinding.root.context).load(ImageUtils.imgPath(userFreeGiftResp.giftVo?.pic))
            .into(mBinding.ivFreeGift)
        val count = userFreeGiftResp.count ?: 0
        if (count == 0) {
            mBinding.tvFreeGiftNum.visibility = View.GONE
        } else {
            mBinding.tvFreeGiftNum.visibility = View.VISIBLE
        }
        mBinding.tvFreeGiftNum.text = count.toString()
    }

}